#include "uid_token_map.h"
#include <boost/lexical_cast.hpp>

namespace imserver {
/*
void UidTokenMap::setUidToken(int64_t uid, const std::string& token) {
  LOG_INFO("UidTokenMap::setUidToken --> uid:" << uid << " token:" << token);
  std::string cmd;
  cmd = "SET " + boost::lexical_cast<std::string>(uid) + ":push_token" + " " + token + " EX 259200";
  redisReply * reply = NULL;
  bool res = false;
  for (int i = 0; i < 2; ++i) {
    reply = redisExecutor.Execute("redis server_key", cmd.c_str());
    if (reply != NULL) {
      if (reply->type == REDIS_REPLY_STATUS && strcasecmp(reply->str, "ok") == 0) {
        res = true;
      }
      freeReplyObject(reply);
      if (res) {
        break;
      }
    }
  }
  if (!res) {
    LOG_ERROR("UidTokenMap::setUidToken --> redis set failed. uid:" << uid << " token:" << token);
  }  
}

std::string UidTokenMap::getTokenByUid(int64_t uid) {
  std::string res = "";
  std::string cmd;
  cmd = "GET " + boost::lexical_cast<std::string>(uid) + ":push_token";
  redisReply * reply = NULL;
  bool redisRes = false;
  for (int i = 0; i < 2; ++i) {
    reply = redisExecutor.Execute("redis server_key", cmd.c_str());
    if (reply != NULL) {
      if (reply->type == REDIS_REPLY_STRING) {
        res.assign(reply->str, reply->len);
        if (res.find("nil") != std::string::npos) {
          res = "";
        }
        redisRes = true;
      }
      freeReplyObject(reply);
      if (redisRes) {
        break;
      }
    }
  }
  if (!redisRes) {
    LOG_ERROR("UidTokenMap::getTokenByUid --> redis get failed. uid:" << uid);
  }
  return res;
}
*/
}
